define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'qingdongwms/finance/staff_wage/index' + location.search,
                    add_url: 'qingdongwms/finance/staff_wage/add',
                    edit_url: 'qingdongwms/finance/staff_wage/edit',
                    del_url: 'qingdongwms/finance/staff_wage/del',
                    multi_url: 'qingdongwms/finance/staff_wage/multi',
                    import_url: 'qingdongwms/finance/staff_wage/import',
                    table: 'qingdongwmswms_warehouse',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'staff_id', title: __('员工'), searchList: $.getJSON("qingdongwms/finance/staff_wage/getstaff"), visible: false },
                        { field: 'staff.name', title: __('员工'), operate: false },
                        { field: 'staff.post', title: __('岗位'), operate: false },
                        { field: 'time', title: __('年月'), operate: 'LIKE' },
                        { field: 'basic_salary', title: __('基本工资'), operate: 'LIKE' },
                        { field: 'overtime_salary', title: __('加班工资'), operate: 'LIKE' },
                        { field: 'social_security', title: __('社保'), operate: 'LIKE' },
                        { field: 'present', title: __('全勤'), operate: 'LIKE' },
                        { field: 'allowance', title: __('津贴'), operate: 'LIKE' },
                        { field: 'bonus', title: __('奖金'), operate: 'LIKE' },
                        { field: 'total_salary', title: __('实发工资'), operate: 'LIKE' },
                        { field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: { 0: __('未发'), 1: __('已发') } },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'),
                            buttons: [{
                                name: 'cashier',
                                text: __('已发'),
                                title: __('已发'),
                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                icon: 'fa fa-magic',
                                url: 'qingdongwms/finance/staff_wage/cashier',
                                confirm: '确认该工资单已发放?',
                                success: function (data, ret) {
                                    table.bootstrapTable('refresh');
                                },
                                visible: function (row) {
                                    if (row.status == 1) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                },
                                error: function (data, ret) {
                                    console.log(data, ret);
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            }],
                            table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],//启用普通表单搜索
                search: false,
                commonSearch: true,
                searchFormVisible: true,
                onLoadSuccess: function () {
                    // 这里就是数据渲染结束后的回调函数
                    $('.btn-editone').html('编辑');
                    $('.fa-pencil').remove();
                    $('.btn-delone').html('删除');
                    $('.fa-trash').remove();
                    $('.btn-editone').removeClass('btn-success');
                    $('.btn-editone').removeClass('btn');
                    $('.btn-delone').removeClass('btn');
                    $('.btn-delone').removeClass('btn-danger');
                }
            });
            $(document).on('click', '.consume-cashier', function (data) {
                var ids = JSON.stringify(Table.api.selectedids(table));
                if (ids == '[]') {
                    Layer.alert('请先选择工资单');
                    return false;
                }
                Layer.confirm(__('确认该工资单将已发?'), {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    Backend.api.ajax({
                        url: "qingdongwms/finance/staff_wage/cashier",
                        data: { ids: ids }
                    }, function (data) {
                        console.log(data, '返回结果')
                        table.bootstrapTable('refresh');
                        Layer.close(index);
                    });
                });
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'qingdongwms/finance/staff_wage/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('Name'), align: 'left' },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'qingdongwms/finance/staff_wage/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'qingdongwms/finance/staff_wage/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});